#!/bin/sh
###
 # @Author: 火红色祥云 809880192@qq.com
 # @Date: 2023-12-26 15:24:39
 # @LastEditors: 火红色祥云 809880192@qq.com
 # @LastEditTime: 2023-12-26 15:30:24
 # @FilePath: /project_luckfox/adc/adc.sh
 # @Description: 要获得 ADC 通道0和通道1实际的电压值，您可以编写一个shell脚本分别读取
 #                  原始电压值 in_voltage0_raw 、 in_voltage1_raw 和比例因子 in_voltage_scale 的值，
 #                  然后分别将两个原始电压值与比例因子相乘即可
### 

echo "Press Ctrl+C to quit"
ADC_DIR="/sys/bus/iio/devices/iio:device0"

while true
do
    scale_value=$(cat "$ADC_DIR/in_voltage_scale")
    IN0_raw_value=$(cat "$ADC_DIR/in_voltage0_raw")
    IN1_raw_value=$(cat "$ADC_DIR/in_voltage1_raw")
    IN0_voltage=$(awk -v raw="$IN0_raw_value" -v scale="$scale_value" 'BEGIN { printf "%.6f\n", raw * scale / 1000 }')
    IN1_voltage=$(awk -v raw="$IN1_raw_value" -v scale="$scale_value" 'BEGIN { printf "%.6f\n", raw * scale / 1000 }')
    echo "IN0_Voltage: $IN0_voltage V,IN1_Voltage: $IN1_voltage V"
    sleep 1
done